package com.ss.android.common.applog;

import android.content.Context;

import com.bytedance.common.utility.Logger;
import com.ss.android.common.util.ToolUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;

final class v extends Thread {
    int a;
    volatile JSONObject tiemSync;
    private final LinkedList c;
    private final Context d;
    private final JSONObject e;
    private final AtomicBoolean f;
    private final com.ss.android.common.applog.AppLog.g g;
    private long h;
    private long i;
    private z j;
    private long k;
    private AtomicLong l;
    private volatile long m;
    private final ConcurrentHashMap n;
    private final ConcurrentHashMap o;
    private final n p;
    private static Context q;
    private static final FilenameFilter r;
    private static final FilenameFilter s;
    private static Thread.UncaughtExceptionHandler t;
    private static final Thread.UncaughtExceptionHandler u;
    private String v;
    private String w;

    static {
        r = new w();
        s = new x();
        u = new y();
    }

    v(Context arg7, JSONObject arg8, LinkedList arg9, AtomicBoolean arg10, AppLog.g arg11, z arg12, ConcurrentHashMap arg13, ConcurrentHashMap arg14) {
        super("LogReaper");
        this.h = 0;
        this.i = 0;
        this.k = 0;
        this.l = new AtomicLong();
        this.a = 0;
        this.tiemSync = null;
        this.m = 120000;
        this.v = null;
        this.w = null;
        this.d = arg7;
        this.e = arg8;
        this.c = arg9;
        this.f = arg10;
        this.g = arg11;
        this.j = arg12;
        this.n = arg13;
        this.o = arg14;
        this.p = new n();
        this.p.a(AppLog.a());
        this.p.a(AppLog.b());
    }

    final void a(long arg2) {
        this.l.set(arg2);
    }

    static void a(Context arg2) {
        if (arg2 != null) {
            q = arg2.getApplicationContext();
            Thread.UncaughtExceptionHandler v0 = Thread.getDefaultUncaughtExceptionHandler();
            t = v0;
            if (v0 == u) {
                t = null;
            } else {
                Thread.setDefaultUncaughtExceptionHandler(u);
            }
        }
    }

    final void a(JSONObject arg6) {
        synchronized (this) {
            try {
                String[] v1 = AppLog.a;
                int v0_2;
                for (v0_2 = 0; v0_2 < 50; ++v0_2) {
                    this.e.put(v1[v0_2], arg6.opt(v1[v0_2]));
                }
            } catch (Exception v0_1) {
                Logger.w("AppLog", "updateHeader exception: " + v0_1);
            }
        }
    }

    private int a(String arg9, String arg10, boolean arg11) throws Throwable {
        // Method was not decompiled
        return 1;
    }

    static Context a() {
        return q;
    }

    private void a(t arg7) {
        LinkedList v1_1;
        s v0_2;
        long v2 = 0;
        synchronized (this) {
            if (arg7 != null) {
                if ((arg7 instanceof u)) {
                    this.a(((u) arg7).a, ((u) arg7).b, ((u) arg7).c, ((u) arg7).d);
                    this.j = ((u) arg7).b;
                    this.k = System.currentTimeMillis();
                } else if ((arg7 instanceof s)) {
                    long v0_1 = ((s) arg7).a;
                    if (v0_1 > v2) {
                        Logger.d("AppLog", "try to batch session  id < " + v0_1);
                        z v1 = com.ss.android.common.applog.l.a(this.d).b(v0_1);
                        if (v1 != null) {
                            this.a(v1, null, false, 0);
                            v0_2 = new s();
                            v0_2.a = v1.a;
                            v1_1 = this.c;
                            synchronized (v1_1) {
                                this.c.add(v0_2);
                            }
                            return;
                        }
                    }
                }

                return;
            }
        }
    }

    private void a(z arg9, z arg10, boolean arg11, long arg12) {
        this.a(arg9, arg10, arg11, arg12, true);
    }

    private void a(z arg13, z arg14, boolean arg15, long arg16, boolean arg18) {
        boolean v1_3 = false;
        int v2_1;
        int v1_2;
        l v0 = com.ss.android.common.applog.l.a(this.d);
        try {
            v0.a(this.e, this.tiemSync);
        } catch (Throwable v1) {
        }

        if (arg13 != null || arg14 != null) {
            if (arg13 != null) {
                long[] v5 = new long[1];
                v5[0] = arg15 ? arg16 : 0;
                com.ss.android.common.applog.AppLog.g v7 = this.g;
                String[] v6 = new String[1];
                long v8 = v0.a(arg13, arg14, this.e, arg15, v5, v6, v7, arg18, this.tiemSync);
                if (v8 <= 0) {
                    return;
                }

                String v1_1 = v6[0];
                if (v5[0] > arg16 && (arg18)) {
                    u v2 = new u();
                    v2.a = arg13;
                    v2.c = true;
                    v2.d = v5[0];
                    LinkedList v3 = this.c;
                    synchronized (v3) {
                        this.c.add(v2);
                    }
                }

                if (!android.support.design.a.isNetworkAvailable(this.d)) {
                    return;
                }

                try {
                    Logger.d("AppLog", "begin to send batch logs");
                    v1_2 = this.a(AppLog.a(), v1_1, true);
                    v2_1 = -1;
                    if (v1_2 == v2_1) {
                        return;
                    }

                    v1_3 = v1_2 == 200 ? true : false;
                    if ((v1_3) && arg14 != null) {
                        if (!this.e()) {
                        } else {
                            arg14.h = true;
                            v0.c(arg14.a);
                        }
                    }
                } catch (Throwable v2_2) {
                    Logger.d("AppLog", "send session exception: " + v2_2);
                }
                v0.a(v8, v1_3);
                if (v1_3) {
                    return;
                }

                if (this.h >= 0) {
                    return;
                }

                this.h = v8;
                return;
            }

            if (arg14 != null && (android.support.design.a.isNetworkAvailable(this.d)) && this.a > 0 && !arg14.g) {
                try {
                    if (!this.e()) {
                        return;
                    }

                    JSONObject v0_2 = new JSONObject();
                    v0_2.put("magic_tag", "ss_app_log");
                    v0_2.put("header", this.e);
                    JSONArray v1_4 = new JSONArray();
                    JSONObject v2_3 = new JSONObject();
                    v2_3.put("datetime", AppLog.b(arg14.c));
                    v2_3.put("session_id", arg14.b);
                    if (arg14.g) {
                        v2_3.put("is_background", true);
                    }

                    v1_4.put(v2_3);
                    v0_2.put("launch", v1_4);
                    this.a(AppLog.a(), v0_2.toString(), true);
                } catch (Throwable v0_1) {
                    Logger.d("AppLog", "send launch exception: " + v0_1);
                }
            }
        }
    }

    final void b(JSONObject arg6) {
        synchronized (this) {
            if (arg6 != null) {
                try {
                    if (arg6.length() != 0) {
                        l v0_2 = com.ss.android.common.applog.l.a(this.d);
                        arg6.put("magic_tag", "ss_app_log");
                        arg6.put("header", this.e);
                        String v1 = arg6.toString();
                        if (Logger.debug()) {
                            Logger.d("AppLog", "insert crash log data: " + v1);
                        }

                        long v0_3 = v0_2.a(v1, 1);
                        if (!Logger.debug()) {
                            return;
                        }
                        Logger.d("AppLog", "insert crash log id: " + v0_3);
                    } else {
                        return;
                    }
                } catch (Throwable v0) {
                    Logger.w("AppLog", "insertCrashlog exception: " + v0);
                }
            }
        }
    }

    static FilenameFilter b() {
        return s;
    }

    static Thread.UncaughtExceptionHandler c() {
        return t;
    }

    static Thread.UncaughtExceptionHandler d() {
        return u;
    }

    private boolean e() {
        boolean v0 = false;
        try {
            String v1_1 = this.e.optString("device_id", "");
            String v2 = this.e.optString("install_id", "");
            if (android.support.design.a.isTextEmpty(v1_1)) {
                return v0;
            }

            if (!android.support.design.a.isTextEmpty(v2)) {
                return true;
            }
        } catch (Throwable v1) {
        }

        return v0;
    }

    private void f() {
        com.ss.android.common.applog.l.a(this.d).b();
    }

    private void g() {
        BufferedReader v3_3;
        long v4_1;
        int v9;
        BufferedReader v6;
        StringBuffer v16;
        File v15;
        BufferedReader v2_1;
        int v8;
        File[] v12;
        Closeable v3 = null;
        try {
            v12 = new File(ToolUtils.getInternalCacheDir(this.d), "ss_native_crash_logs").listFiles(r);
            int v14;
            String v13;
            if (v12 != null && v12.length > 0) {
                Arrays.sort(((Object[]) v12), Collections.reverseOrder());
                v13 = this.w;
                this.w = v12[0].getName();
                v14 = v12.length;
                v8 = 0;
                v2_1 = ((BufferedReader) v3);
                v3 = null;
            } else {
                return;
            }

            while (true) {
                if (v8 < v14) {
                    try {
                        v15 = v12[v8];
                        Closeable v7;
                        if (v8 >= 5) {
                            v7 = null;
                        } else if (v13 != null) {
                            if (!v13.equals(v15.getName())) {
                                v7 = v3;
                            } else {
                                v7 = null;
                            }
                        } else {
                            v7 = null;
                        }
                        long v10 = 0;
                        v16 = new StringBuffer();
                        BufferedReader v4 = null;
                        if (v7 != null) {
                            v3_3 = v2_1;
                        } else if (v15.length() >= 16384) {
                            v3_3 = v2_1;
                        } else {
                            try {
                                v6 = new BufferedReader(new FileReader(v15));
                                v9 = 0;
                                v2_1 = v4;
                                v4_1 = v10;
                                String v2_3 = null;
                                while (true) {
                                    String v3_4 = v6.readLine();
                                    if (v3_4 == null) {
                                        break;
                                    }
                                    if (v9 == 0) {
                                        v4_1 = Long.parseLong(v3_4);
                                    } else if (v9 == 1) {
                                        v2_3 = v3_4;
                                    } else {
                                        v16.append(v3_4 + "\n");
                                    }
                                    ++v9;
                                }
                                v6.close();
                                v3_3 = null;
                                JSONObject v6_1 = new JSONObject();
                                v6_1.put("data", v16.toString().trim());
                                v6_1.put("is_native_crash", 1);
                                if (!v2_3.startsWith("no_process_name")) {
                                    v6_1.put("process_name", v2_3);
                                }
                                if (v4_1 > 0) {
                                    v6_1.put("crash_time", v4_1);
                                }
                                if (v2_3 == null || !v2_3.contains(":")) {
                                    v6_1.put("remote_process", 0);
                                } else {
                                    v6_1.put("remote_process", 1);
                                }
                                this.b(v6_1);
                            } catch (Exception v3_2) {
                                v3_3 = v2_1;
                            }
                        }
                        v15.delete();
                        ++v8;
                        v2_1 = v3_3;
                        v3 = v7;
                        continue;
                    } catch (Throwable v2) {
                        Logger.w("AppLog", "parse native crash log exceptin: " + v2);
                        break;
                    } finally {
                        android.support.design.a.closeQuietly(((Closeable) v2_1));
                    }
                } else {
                    android.support.design.a.closeQuietly(((Closeable) v2_1));
                    return;
                }
            }
        } catch (Exception e) {

        }
    }

    private boolean h() {
        boolean v0_5;
        String v1_3;
        BufferedReader v0_3 = null;
        File v7;
        int v0_2;
        int v3;
        File[] v4;
        int v2;
        try {
            v4 = new File(ToolUtils.getInternalCacheDir(this.d), "ss_crash_logs").listFiles(s);
            if (v4 != null && v4.length > 0) {
                Arrays.sort(((Object[]) v4), Collections.reverseOrder());
                String v5 = this.v;
                this.v = v4[0].getName();
                int v6 = v4.length;
                v3 = 0;
                v0_2 = 0;
                while (true) {
                    if (v3 >= v6) {
                        break;
                    }
                    v7 = v4[v3];
                    if (v3 < 5) {
                        if (v5 != null) {
                            if (!v5.equals(v7.getName())) {
                                v2 = v0_2;
                            } else {
                                v2 = 1;
                            }
                        } else {
                            v2 = v0_2;
                        }
                    } else {
                        v2 = 1;
                    }
                    if (v2 != 0) {

                    } else if (v7.length() >= 16384) {

                    } else {
                        v0_3 = new BufferedReader(new FileReader(v7));
                        v1_3 = v0_3.readLine();
                        v0_3.close();
                        this.b(new JSONObject(v1_3));
                    }
                    v7.delete();
                    ++v3;
                    v0_2 = v2;
                    continue;
                }
            }
        } catch (Exception v0) {
        } finally {
            android.support.design.a.closeQuietly(v0_3);
        }
        this.g();
        if (!android.support.design.a.isNetworkAvailable(this.d)) {
            v0_5 = false;
            return v0_5;
        }

        if (this.h < 0 && System.currentTimeMillis() - this.i > this.m) {
            this.h = 0;
            this.f();
            this.i = System.currentTimeMillis();
        }

        if (this.h < 0) {
            return false;
        }

        l v3_1 = com.ss.android.common.applog.l.a(this.d);
        com.ss.android.common.applog.q v4_1 = v3_1.a(this.h);
        if (v4_1 == null) {
            this.h = -1;
            return false;
        }

        if (this.h < v4_1.a) {
            this.h = v4_1.a;
        } else {
            ++this.h;
        }

        if (v4_1.b == null || v4_1.b.length() == 0) {
            return true;
        }

        v2 = 0;
        int v1_4 = 0;
        try {
            if (v4_1.c == 0) {
                v1_4 = this.a(AppLog.a(), v4_1.b, true);
            } else if (v4_1.c == 1) {
                v1_4 = this.a(AppLog.b(), v4_1.b, false);
            } else if (v4_1.c == 2) {
                v1_4 = this.a(AppLog.c(), v4_1.b, false);
            } else {
                v1_4 = 200;
            }
            v0_2 = v1_4 == 200 ? 1 : 0;
            int v12_1 = v1_4;
            v1_4 = v0_2;
            v0_2 = v12_1;
        } catch (Throwable v0_1) {
            Logger.d("AppLog", "send session exception: " + v0_1);
            v0_2 = v1_4;
            v1_4 = v2;
        }
        v2 = -1;
        if (v0_2 == v2) {
            return true;
        }
        v3_1.a(v4_1.a, v1_4 > 0);
        return true;
    }

    public final void run() {
        LinkedList v1;
        Logger.d("AppLog", "LogReaper start");
        this.f();
        Object v7 = null;
        this.i = System.currentTimeMillis();
        this.k = System.currentTimeMillis();
        int v10 = 0;
        while (true) {
            if (v7 == null) {
                try {
                    v1 = this.c;
                    synchronized (v1) {
                        if (this.f.get()) {
                            Logger.d("AppLog", "LogReaper quit");
                            return;
                        } else {
                            if (!this.c.isEmpty()) {
                                v7 = this.c.poll();
                            }
                        }
                    }
                } catch (Throwable v0) {
                    break;
                }
            }

            if (v7 != null) {
                this.a(((t) v7));
                v7 = null;
                continue;
            }

            long v0_1 = this.l.get();
            long v8 = v0_1 < 20000 ? 0 : v0_1;
            z v1_1 = this.j;
            long v2 = v1_1 == null || (v1_1.g) ? 0 : v1_1.a;
            long v4 = System.currentTimeMillis();
            if (v8 <= 0 || v2 <= 0) {
                v8 = 0;
            } else if (v4 - this.k > v8) {
                if (android.support.design.a.isNetworkAvailable(this.d)) {
                    this.k = v4;
                    Logger.v("AppLog", "batch event " + v8);
                    this.a(v1_1, null, true, 0, false);
                } else {
                    v8 = 0;
                }
            }

            if (this.h()) {
                if (!AppLog.s()) {
                    continue;
                }

                int v0_2 = v10 + 1;
                if (v0_2 > 4) {
                    this.h = -1;
                } else {
                    v10 = v0_2;
                    continue;
                }
            }

            LinkedList v2_1 = this.c;
            synchronized (v2_1) {
                boolean bingo = false;
                Object v0_4;
                try {
                    if (!this.c.isEmpty()) {
                        v0_4 = this.c.poll();
                        v10 = 0;
                        v7 = v0_4;
                        continue;
                    }

                    if (v8 <= 0) {
                        this.c.wait();
                    } else {
                        Logger.v("AppLog", "wait for batch event " + v8);
                        this.c.wait(v8);
                    }
                    bingo = true;
                } catch (InterruptedException e) {
                    bingo = true;
                } catch (Exception ex) {

                }
                if (bingo) {
                    if (!this.f.get()) {
                        v0_4 = v7;
                        v10 = 0;
                        v7 = v0_4;
                        continue;
                    } else {
                        Logger.d("AppLog", "LogReaper quit");
                        return;
                    }
                }
            }
        }
    }
}

